#include "optionunit.h"
#include "ui_optionunit.h"
#include <QVBoxLayout>
#include <QToolButton>
#include "optionitem.h"
#include <QToolButton>
#include <QDebug>
#include <QLabel>
#include <optiongroup.h>
#include <QFile>
#include <QToolButton>
#include "optionadd.h"
OptionUnit::OptionUnit(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::OptionUnit)
{

}
void OptionUnit::update(OptionGroup group,bool commit){

    // 计算所有的操作组所有的操作数量，方便展示图标状态
    totalOptions = group.group.count();

    ui->setupUi(this);
    if(commit)
    {
        ui->btn_add->setParent(0);
        ui->horizontalLayout->removeWidget(ui->btn_add);
    }
    else{
        connect(ui->btn_add,&QToolButton::clicked,this,[=](){
            OptionAdd* optionAdd = new OptionAdd;
            optionAdd->setTime(group.group.at(0).datetime);
            optionAdd->show();
            connect(optionAdd,&OptionAdd::completed,this,[=](){
               emit addSignal();
            });
            qDebug()<<"添加新的操作子项";
        });
    }
//    ui->verticalLayout
    // 加载所有的操作项
    QVBoxLayout* vlayout = new QVBoxLayout(ui->widget);
    for(int i = 0;i<totalOptions;i++)
    {
        Option optItem = group.group.at(i);
        OptionItem* item = new OptionItem(ui->widget);
        item->update(optItem);
        connect(item,&OptionItem::changeStatus,this,&OptionUnit::handleItemStatusChange);
        connect(this,&OptionUnit::selectAll,item,&OptionItem::setSelected);
        connect(this,&OptionUnit::clearSelect,item,&OptionItem::clearSelected);
        connect(item,&OptionItem::deleteSignal,this,[=](){
            totalOptions--;
            sql->optionDelete(optItem.id);
            if(totalOptions==0){
                emit deleteSignal();
            }
            else{
                item->close();
                ui->widget->adjustSize();
                this->adjustSize();
                emit sizeChanged();
            }
        });
        vlayout->addWidget(item);
    }
    ui->btn_title->setText(group.dateTime.toString("yyyy 年 MM 月 dd 日 hh:mm:ss"));
    connect(ui->btn_title,&QToolButton::clicked,this,&OptionUnit::changeStatus);
    this->status = 0;
    connect(ui->btn_check,&QToolButton::clicked,[=](){
        if(status!=0)
        {
            status = 0;
            ui->btn_check->setIcon(QIcon(":/img/res/img/checkBox_empty.png"));
            emit clearSelect();

        }
        else{
            status = 2;
            ui->btn_check->setIcon(QIcon(":/img/res/img/checkBox_all.png"));
            emit selectAll();
        }
    });

    ui->widget->setVisible(!hide);
    ui->widget->adjustSize();
    this->adjustSize();

}
void OptionUnit::deleteSelectedOption(){
    qDebug()<<"要删除的id"<<selected;
    for (int i =0;i<selected.size();i++)
    {
        long id = selected.at(i);
        totalOptions--;
        sql->optionDelete(id);
    }
    selected.clear();

}

void OptionUnit::commitSelectedOption(){
    qDebug()<<"要提交的id"<<selected;
    sql->optionCommit(selected);
    selected.clear();
    emit commitSignal();

}
void OptionUnit::changeStatus(){
   hide = !hide;
   ui->widget->setVisible(!hide);
   if(hide){
       ui->widget->adjustSize();
       this->adjustSize();
       ui->btn_title->setIcon(QIcon(":/img/res/img/fold.png"));
   }
   else{
       ui->widget->adjustSize();
       this->adjustSize();
       ui->btn_title->setIcon(QIcon(":/img/res/img/unfold.png"));
   }
   emit sizeChanged();
}
void OptionUnit::handleItemStatusChange(short status,long id){
    if(status == 0)
    {
        if(selected.contains(id))
        {
            selected.removeOne(id);
            if(selected.count()==0)
            {
                ui->btn_check->setIcon(QIcon(":/img/res/img/checkBox_empty.png"));
            }
            else if(selected.count()==totalOptions){
                ui->btn_check->setIcon(QIcon(":/img/res/img/checkBox_all.png"));
            }
            else{
                ui->btn_check->setIcon(QIcon(":/img/res/img/checkBox_notAll.png"));
            }
        }
    }
    else
    {
        if(!selected.contains(id)){
            selected.append(id);
            if(selected.count()==0)
            {
                ui->btn_check->setIcon(QIcon(":/img/res/img/checkBox_empty.png"));
            }
            else if(selected.count()==totalOptions){
                ui->btn_check->setIcon(QIcon(":/img/res/img/checkBox_all.png"));
            }
            else{
                ui->btn_check->setIcon(QIcon(":/img/res/img/checkBox_notAll.png"));
            }
        }
    }
}
OptionUnit::~OptionUnit()
{
    delete ui;
}
